<?php
/**
 * Description of controller
 *
 * @author Christian Simon
 */
class controllerRank {

    public static function get($int, $type) {
        if ($int != null) {
            switch ($type) {
                case "Guild":
                    $sql = "select `name` from `guild_rank` where `_id` = " . $int;
                    break;
                case "RP":
                    $sql = "select `name` from `roleplay_rank` where `_id` = " . $int;
                    break;
            }
            $result = MySql::query($sql);
            if ($result != "null") {
                $row = mysql_fetch_row($result);
                $rank = new modelRank();
                return $rank->deserialize($row);
            }
        }
    }

    public static function add($rank, $type) {
        switch ($type) {
            case "Guild":
                $sql = "INSERT INTO `vdl_roleplay`.`guild_rank` (`_id`, `_oid`, `_rid`, `name`) 
            VALUES (NULL," .
                        $rank->_oid . ", " .
                        $rank->_rid . ", \"" .
                        $rank->name . "\");";
                break;
            case "RP":
                $sql = "INSERT INTO `vdl_roleplay`.`roleplay_rank` (`_id`, `_oid`, `_rid`, `name`) 
            VALUES (NULL," .
                        $rank->_oid . ", " .
                        $rank->_rid . ", \"" .
                        $rank->name . "\");";
                break;
        }
        MySql::insert($sql);
        return mysql_affected_rows();
    }

    public static function delete($int, $type) {
        switch ($type) {
            case "Guild":
                $sql = "DELETE FROM  `guild_rank` where `_id` = " . $int;
                break;
            case "RP":
                $sql = "DELETE FROM  `roleplay_rank` where `_id` = " . $int;
                break;
        }
        MySql::query($sql);
        return mysql_affected_rows();
    }

    private static function getMemberCount($int,$type) {
        switch ($type) {
            case "Guild":
                $sql = "Select count(*) from `character` where `_rid` = " . $int;
                break;
            case "RP":
                $sql = "Select count(*) from `character_roleplay` where `_rid` = " . $int;
                break;
        }
        $query = MySql::query($sql);
        $row = mysql_fetch_row($query);
        return $row[0];
    }

    public static function getNameById($int, $type) {
        $sql = "Select `name` from `rank` where `_id` = " . $int;
        $query = MySql::query($sql);
        if (empty($query)) {
            return "-";
        } else {
            $row = mysql_fetch_row($query);
            return $row[0];
        }
    }

    public static function getAdminRow() {
        $sql = "SELECT * from `rank`";
        $query = MySql::query($sql);
        while ($row = mysql_fetch_object($query)) {
            echo "<tr>";
            echo "<td>";
            echo htmlentities($row->name, ENT_QUOTES, 'UTF-8');
            echo "</td><td>";
            echo htmlentities(controllerOrganisation::getNameById($row->_oid), ENT_QUOTES, 'UTF-8');
            echo "</td><td>";
            echo self::getMemberCount($row->_id);
            echo "</td><td>";
            echo htmlentities(self::getNameById($row->_rid), ENT_QUOTES, 'UTF-8');
            echo "</td><td>";
            echo "<a href='#' onclick='loadTabContent(\"./view/admin.rank.edit.php\",\"" . $row->_id . "\")'><img title='" . htmlentities($row->name, ENT_QUOTES, 'UTF-8') . " bearbeiten' src='./res/icons/pencil.png'></a>";
            echo "</td><td>";
            echo "<a href='#' onclick='removeRank(" . $row->_id . ")'><img title='" . htmlentities($row->name, ENT_QUOTES, 'UTF-8') . " löschen' src='./res/icons/close.png'></a>";
            echo "</td>";
            echo "</tr>";
        }
    }

}

?>
